package com.jlastu.znb.view;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jlastu.znb.adapter.SalesListAdapter;
import com.jlastu.znb.contract.SearchContract;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.jlastu.znb.R;
import com.jlastu.znb.entity.Goods;
import com.jlastu.znb.presenter.SearchPresenter;

import java.util.ArrayList;
import java.util.List;

public class SearchActivity extends AppCompatActivity implements SearchContract.ISearchView {
    private RecyclerView mGoodsRecyclerView;
    private SalesListAdapter mRecyclerAdapter;
    private SearchContract.ISearchPresenter mSearchPresenter;
    private List<Goods> mGoodsList;
    private ImageView mBack;
    private TextView mTextView1;
    private TextView mTextView2;
    private TextView mTextView3;
    private TextView mSearchButton;
    private EditText mSearchText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        mSearchPresenter = new SearchPresenter(this, this);
        mGoodsList = new ArrayList<>();
        mGoodsRecyclerView = findViewById(R.id.listView);
        StaggeredGridLayoutManager staggeredGridLayoutManager =
                new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
        mGoodsRecyclerView.setLayoutManager(staggeredGridLayoutManager);
        mRecyclerAdapter = new SalesListAdapter(mGoodsList);
        mGoodsRecyclerView.setAdapter(mRecyclerAdapter);

        mSearchButton = findViewById(R.id.search_button);
        mSearchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取搜索框EditText
                mSearchText = findViewById(R.id.search);
                String query = mSearchText.getText().toString();
                // 如果搜索框有文本，则调用searchGoodsByName方法
                if (!query.isEmpty()) {
                    mSearchPresenter.searchGoodsByName(query);
                }
            }
        });

        mTextView1 = findViewById(R.id.search_title1);
        mTextView2 = findViewById(R.id.search_title2);
        mTextView3 = findViewById(R.id.search_title3);

        // 初始化时默认选中第一个标签
        setTabSelected(mTextView1);

        // 设置点击事件
        mTextView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setTabSelected(mTextView1);
                mSearchPresenter.getGoodsList();
            }
        });
        mTextView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setTabSelected(mTextView2);
            }
        });
        mTextView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setTabSelected(mTextView3);
            }
        });
        mBack = findViewById(R.id.back);
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SearchActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }

    private void setTabSelected(TextView selectedTab) {
        // 重置所有标签为未选中状态
        mTextView1.setSelected(false);
        mTextView2.setSelected(false);
        mTextView3.setSelected(false);

        // 将被点击的标签设置为选中状态
        selectedTab.setSelected(true);
    }

    @Override
    public void onResume() {
        super.onResume();
        mSearchPresenter.getGoodsList(); // 获取全部商品列表
    }

    @Override
    public void showGoodsList(List<Goods> goodsList) {
        // 更新商品列表并标记为非首次加载
        mGoodsList.clear();
        mGoodsList.addAll(goodsList);
        Log.d("SearchActivity", "showGoodsList size: " + mGoodsList.size());
        mRecyclerAdapter.notifyDataSetChanged();
    }

    @Override
    public void searchGoodsByName(List<Goods> goodsList) {

    }

}